<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="登录邮箱" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="登录邮箱"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="comfirmPassword" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.comfirmPassword" type="password" placeholder="确认密码"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="学院" prop="college">
        <!-- <el-input v-model="dataForm.college" placeholder="学院"></el-input> -->
        <el-select v-model="dataForm.college" clearable placeholder="请选择学院">
          <el-option v-for="item in collegeList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="角色" size="mini" prop="roleIdList">
        <el-radio v-for="(k) in roleList" :key="k.roleId" v-model="dataForm.roleIdList[0]" :label="k.roleId">
          {{ k.roleName }}
        </el-radio>
      </el-form-item>
      <!--      <el-form-item v-if="dataForm.roleIdList[0]==1" label="专家等级" prop="expertStatu">
        <el-radio-group v-model="dataForm.expertStatu">
          <el-radio :label="1" >{{'院级专家'}}</el-radio>
          <el-radio :label="2" v-if="roleId!=2">{{'校级专家'}}</el-radio>
        </el-radio-group>

      </el-form-item> -->
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  isEmail,
  isMobile
} from '@/utils/validate'
export default {
  data() {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    var validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('确认密码不能为空'))
      } else if (this.dataForm.password !== value) {
        callback(new Error('确认密码与密码输入不一致'))
      } else {
        callback()
      }
    }
    var validateEmail = (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error('邮箱格式错误'))
      } else {
        callback()
      }
    }
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return {
      roleId: this.$cookie.get("roleId"),
      collegeList: [
        '数学学院',
        '物理与电子工程学院',
        '化学与环境学院',
        '文学院',
        '外国语学院',
        '生命科学学院',
        '政法学院',
        '地理科学与旅游学院',
        '经济与管理学院',
        '计算机学院',
        '土木工程学院',
        '林风眠美术学院',
        '体育学院',
        '音乐与舞蹈学院',
        '教育科学学院',
        '医学院'
      ],
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        userName: '',
        password: '',
        comfirmPassword: '',
        salt: '',
        name: '',
        mobile: '',
        college: '',
        roleIdList: [],
        status: 1,
        // expertStatu:'',
      },
      dataRule: {
        userName: [{
          required: true,
          validator: validateEmail,
          trigger: 'blur'
        }],
        password: [{
          // required: true,
          validator: validatePassword,
          trigger: 'blur'
        }],
        college: [{
          required: true,
          message: '学院不能为空',
          trigger: 'blur'
        }],
        comfirmPassword: [{
          validator: validateComfirmPassword,
          trigger: 'blur'
        }],
        name: [{
          required: true,
          message: '姓名不能为空',
          trigger: 'blur'
        },
          // { validator: validateEmail, trigger: 'blur' }
        ],
        mobile: [{
          required: true,
          message: '手机号不能为空',
          trigger: 'blur'
        },
        {
          validator: validateMobile,
          trigger: 'blur'
        }
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/sys/role/select'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({
        data
      }) => {
        this.roleList = data && data.code === 0 ? data.list : []
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.dataForm.userName = data.user.username
              this.dataForm.salt = data.user.salt
              this.dataForm.email = data.user.email
              this.dataForm.mobile = data.user.mobile
              this.dataForm.roleIdList = data.user.roleIdList
              this.dataForm.status = data.user.status
              this.dataForm.college = data.user.college
              this.dataForm.name = data.user.name
              // this.dataForm.expertStatu=data.user.expertStatu
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/user/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'userId': this.dataForm.id || undefined,
              'username': this.dataForm.userName,
              'password': this.dataForm.password,
              'salt': this.dataForm.salt,
              'email': this.dataForm.email,
              'mobile': this.dataForm.mobile,
              'status': this.dataForm.status,
              'roleIdList': this.dataForm.roleIdList,
              'college': this.dataForm.college,
              'name': this.dataForm.name,
              // 'expertStatu':this.dataForm.expertStatu
            })
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
